package com.richard.home.ui.fragment

import android.os.Handler
import android.widget.Toast
import androidx.datastore.preferences.SharedPreferencesMigration
import androidx.datastore.preferences.createDataStore
import androidx.datastore.preferences.edit
import androidx.datastore.preferences.preferencesKey
import androidx.dynamicanimation.animation.DynamicAnimation
import androidx.dynamicanimation.animation.SpringAnimation
import androidx.dynamicanimation.animation.SpringForce
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.badlogic.gdx.backends.android.AndroidFragmentApplication
import com.richard.base.dialog.ConfirmDialog
import com.richard.base.view.gdxView.view.GdxView
import com.richard.base.route.RouterPath
import com.richard.base.ui.BaseVMFragment
import com.richard.base.util.ImageLoader
import com.richard.base.view.gdxView.adapter.GdxAdapter
import com.richard.home.R
import com.richard.home.viewModel.HomeMineViewModelImpl
import kotlinx.android.synthetic.main.home_mine_fragment.*
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import org.koin.android.viewmodel.ext.android.getViewModel

@Route(path = RouterPath.Mine.KS_MINE_PAGER)
class HomeMineFragment : BaseVMFragment<HomeMineViewModelImpl>(), AndroidFragmentApplication.Callbacks {


    override fun getLayoutResId(): Int = R.layout.home_mine_fragment


    override fun initView() {
        btn_start.setOnClickListener {
            iv_image.scaleX = 1.2f
            iv_image.scaleY = 1.2f
            iv_image.pivotX = iv_image.measuredWidth / 2f
            iv_image.pivotY = iv_image.measuredHeight.toFloat()
            val animationX = SpringAnimation(iv_image, DynamicAnimation.SCALE_X,1f).apply {
                //阻尼比
                spring.dampingRatio = SpringForce.DAMPING_RATIO_HIGH_BOUNCY
                //刚度
                spring.stiffness = SpringForce.STIFFNESS_LOW
            }
            animationX.start()

            val animationY = SpringAnimation(iv_image, DynamicAnimation.SCALE_Y,1f).apply {
                //阻尼比
                spring.dampingRatio = SpringForce.DAMPING_RATIO_HIGH_BOUNCY
                //刚度
                spring.stiffness = SpringForce.STIFFNESS_LOW
            }
            animationY.start()

//            ConfirmDialog(context!!, "关闭", null ).show()
            Toast.makeText(context, "Toast Show", Toast.LENGTH_SHORT).show()
        }


        vwv_voice.setLines(25)

        btn_start_wave.setOnClickListener {
            vwv_voice.start(18 * 1000)
        }

        btn_data_store.setOnClickListener {
            val DATASTORE_PREFERENCE_NAME = "DataStorePreference"//定义 DataStore 的名字
            val OLD_SP_PREFERENCE_NAME = "old_shared_preference_name"//旧的存储迁移
            val mDataStorePre = context?.createDataStore(
                name = DATASTORE_PREFERENCE_NAME,
                migrations = listOf(SharedPreferencesMigration(context!!, OLD_SP_PREFERENCE_NAME))
            )

            val PREFERENCE_KEY_NAME = "PREFERENCE_KEY_NAME" //定义存储的key
            var preKey = preferencesKey<String>(PREFERENCE_KEY_NAME)

            lifecycleScope.launch {
                mDataStorePre?.edit { mutablePreferences ->
                    mutablePreferences[preKey] = "存储的value"
                }
            }
            lifecycleScope.launch {
                var valueFlow = mDataStorePre?.data?.map { preferences ->
                    preferences[preKey] ?: ""
                }
                val value = valueFlow?.first()
                tv_data_store_test.text = value
            }

        }



    }


    override fun initData() {
        context?.let {
            val atlasPath = "rabbit/tuzi.atlas"
            val jsonPath = "rabbit/tuzi.json"

            val header = GdxView(it, childFragmentManager,atlasPath, jsonPath)
            ll_ip.addView(header)

            Handler().postDelayed({
                header.startAnimation("animation", true, object : GdxAdapter.ActionCompleteListener{
                    override fun onComplete() {
                    }
                })
            }, 2000)
        }
    }



    override fun startObserve() {

    }



    companion object {
        @JvmStatic
        fun newInstance() = HomeMineFragment()
    }

    override fun initVm(): HomeMineViewModelImpl {
        return getViewModel()
    }

    override fun exit() {

    }

}